//
//  UIImageView+Extension.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/11/27.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//

#import "UIImageView+Extension.h"

@implementation UIImageView (Extension)

- (void)setCircleHeader:(NSString *)url
{
    UIImage *placeHolderImage = [UIImage originImageWithName:@"placeholder_monkey_round_40"];
    
    @weakify(self)
    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeHolderImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
        @strongify(self)
        if(!image){
            
            return ;
        }
        self.image = [image circleImage];
    }];
}

+ (UIImage *)createImageWithColor:(UIColor *)color size:(CGSize)size
{
    if(size.width == 0 || size.height == 0){
        size = CGSizeMake(1, 1);
    }
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    // 开启图形上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获取当前的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 填充颜色
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 填充整个区域
    CGContextFillRect(context, rect);
    
    UIImage *createImage = UIGraphicsGetImageFromCurrentImageContext();
    // 关闭图形上下文
    UIGraphicsEndImageContext();
    return createImage;
}

@end
